<?php

namespace App\Http\Controllers\WorkOrder\Wap;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\MerchantAppAuth;
use App\Http\Validate\WorkOrder\FunctionOpen\FunctionOpenValidate;
use App\Rules\Work\CheckCertificates;
use App\Service\FuYou\FuYouService;
use App\Service\WorkOrder\FunctionOpen\FunctionOpenService;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class FunctionOpenController extends BaseController
{

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws Exception|GuzzleException
     */
    public function preview(Request $request): JsonResponse
    {
        $shopId = $request->header('shopId');
        return $this->success(FunctionOpenService::preview($shopId));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws Exception
     */
    public function formContent(Request $request): JsonResponse
    {
        $shopId        = $request->header('shopId');
        $requestParams = FunctionOpenValidate::formContentValidate($request->all());
        $uid           = MerchantAppAuth::$userId;
        return $this->success(FunctionOpenService::formContent($requestParams->type, $shopId, $uid));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws Exception
     */
    public function saveField(Request $request): JsonResponse
    {
        $requestParams = FunctionOpenValidate::saveFieldValidate($request->all());
        FunctionOpenService::saveField(
            $requestParams->id,
            $requestParams->field,
            $requestParams->value
        );
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws Exception
     */
    public function saveFields(Request $request): JsonResponse
    {
        $requestParams = FunctionOpenValidate::saveFieldsValidate($request->all());
        return $this->success(FunctionOpenService::saveFields($requestParams->id, $requestParams->formFields));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws Exception
     */
    public function submitForm(Request $request): JsonResponse
    {
        $this->validate($request, [
            'id' => new CheckCertificates()
        ]);
        $requestParams = FunctionOpenValidate::submitFormValidate($request->all());
        $uid           = MerchantAppAuth::$userId;
        return $this->success(FunctionOpenService::submitForm($requestParams->id, $uid));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function fyRegionBank(Request $request): JsonResponse
    {
        $requestParams = FunctionOpenValidate::fyRegionBankValidate($request->all());
        return $this->success(FunctionOpenService::fyRegionBank($requestParams));
    }


    /**
     * 富友微信/支付宝渠道认证获取二维码
     * @param Request $request
     * @param string $workOrderNo
     * @return JsonResponse
     */
    public function getChannelAuthorize(Request $request, string $workOrderNo): JsonResponse
    {
        return $this->success(FuYouService::getChannelAuthorize(['workOrderNo' => $workOrderNo]));
    }

    /**
     * 富友微信/支付宝渠道认证确认
     * @param Request $request
     * @param string $workOrderNo
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function channelAuthorize(Request $request, string $workOrderNo): JsonResponse
    {
        return $this->success(FuYouService::channelAuthorize(['workOrderNo' => $workOrderNo]));
    }

}
